package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.core.domain.entity.Warning;

/**
 * 短信工具类
 *
 * @author ruoyi
 */
public class SmsUtils {
    public static CommonResponse sendSms(String templateCode, String phoneNumbers, String jsonObject) {
        /**
         * 连接阿里云：
         *
         * 三个参数：
         * regionId 不要动，默认使用官方的
         * accessKeyId 自己的用户accessKeyId
         * accessSecret 自己的用户accessSecret
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hu", "LTAI5tMFdWdEgccQFMpbA165", "szT7a0nqx757pgQW11yGflN6mnjV78");
        IAcsClient client = new DefaultAcsClient(profile);

        // 构建请求：
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        // 自定义参数：
        request.putQueryParameter("PhoneNumbers", phoneNumbers);// 接收短信的手机号
        request.putQueryParameter("SignName", "元泰云");// 短信签名
        request.putQueryParameter("TemplateCode", templateCode);// 短信模版CODE
        request.putQueryParameter("TemplateParam", jsonObject);//传参

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
